﻿// $Id: Question.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 Noury Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*! \file
 * \brief Definition of \c Salamanca.DataActivities.UI.Question.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 févr. 2008) $
 * \version $Revision: 10 $
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace Salamanca.DataActivities.UI
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Interface implemented by a type that contains an answer to a <see cref="Question" />.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public interface IAnswerContainer<T>
    {
        /// <summary>Gets or sets the answer.</summary>
        T Answer
        {
            get;
            set;
        }
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Arguments for a question anwer event.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public sealed class AnswerEventArgs:
        EventArgs
    {

        /// <summary>Initializes a new instance of the <see cref="AnswerEventArgs"/> class.</summary>
        public AnswerEventArgs(object data)
        {
            _Data=data;
        }

        /// <summary>Initializes a new instance of the <see cref="AnswerEventArgs"/> class.</summary>
        public AnswerEventArgs(object answer, object data):
            this(data)
        {
            _Answer=answer;
        }

        /// <summary>Gets or sets the answer.</summary>
        /// <value>The answer.</value>
        public object Answer
        {
            get
            {
                return _Answer;
            }
            set
            {
                _Answer=value;
            }
        }

        /// <summary>Gets the activity data.</summary>
        /// <value>The activity data.</value>
        public object Data
        {
            get
            {
                return _Data;
            }
        }

        /// <summary>The answer.</summary>
        private object _Answer;
        /// <summary>The activity data.</summary>
        private object _Data;
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Implementation of an answered question.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class AnsweredQuestion:
        Question
    {

        /// <summary>Initializes a new <see cref="AnsweredQuestion"/> instance</summary>
        public AnsweredQuestion()
        {
        }

        /// <summary>Asks the question.</summary>
        public override ActivityStateResult Ask(IActivityController controller)
        {
            return ActivityStateResult.Next;
        }
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Implementation of an answered question.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class AnsweredQuestion<TAnswer>:
        AnsweredQuestion
    {

        /// <summary>Initializes a new <see cref="AnsweredQuestion{TAnswer}"/> instance</summary>
        public AnsweredQuestion(TAnswer answer)
        {
            _Answer=answer;
        }

        /// <summary>Raises the <see cref="Question.Answering" /> event.</summary>
        protected override void OnAnswering(AnswerEventArgs e)
        {
            e.Answer=_Answer;

            base.OnAnswering(e);

            IDataBag<TAnswer> data=e.Data as IDataBag<TAnswer>;
            if (data!=null)
                data.Data=(TAnswer)e.Answer;
        }

        /// <summary>The answer.</summary>
        private TAnswer _Answer;
    }

}
